//
//  LLoadingView.m
//  LLoadingDemo
//
//  Created by 李明明 on 16/10/25.
//  Copyright © 2016年 李明明. All rights reserved.
//

#import "LLoadingView.h"

@interface LLoadingView ()
@property (nonatomic, weak) IBOutlet UIButton *btn;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end

@implementation LLoadingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.btn];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self addSubview:self.btn];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat width = CGRectGetWidth(rect);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextAddArc(context, width / 2, width / 2, width / 2 - 1, 0, 2 * M_PI, NO);
    CGContextDrawPath(context, kCGPathStroke);
}

- (void)animateAndLine
{
    if (!self.shapeLayer) {
        CGFloat width = CGRectGetWidth(self.bounds);
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path addArcWithCenter:CGPointMake(0, 0) radius:width / 2 - 1 startAngle:-M_PI_2 endAngle:0 clockwise:YES];
        self.shapeLayer = [CAShapeLayer layer];
        [self.layer addSublayer:self.shapeLayer];
        self.shapeLayer.position = CGPointMake(width / 2, width / 2);
        self.shapeLayer.strokeColor = [UIColor greenColor].CGColor;
        self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
        self.shapeLayer.lineWidth = 2;
        self.shapeLayer.path = path.CGPath;
    }
    [self startAnimate];
}

- (void)startAnimate
{
    CABasicAnimation *animate = [CABasicAnimation animation];
    animate.keyPath = @"strokeStart";//@"transform.rotation.z";
    animate.toValue = @(M_PI / 2);
    
    CABasicAnimation *animate1 = [CABasicAnimation animation];
    animate1.keyPath = @"transform.rotation.z";
    animate1.toValue = @(M_PI * 2);
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animate,animate1];
    group.duration = 2;
    group.repeatCount = MAX_CANON;
    
    [self.shapeLayer addAnimation:group forKey:nil];
    
}

- (void)endAnimate
{
    [self.shapeLayer removeAllAnimations];
}

- (UIButton *)btn
{
    if (!_btn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        [self addSubview:btn];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"click" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.cornerRadius = btn.bounds.size.width / 2;
        _btn = btn;
    }
    return _btn;
}

- (void)clickBtn:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected) {
        [self animateAndLine];
    }else {
        [self endAnimate];
    }
}




@end
